unit uExportDecl;

interface
  uses windows, Classes, SysUtils, Forms, Dialogs, Graphics, StdCtrls
  ;

  type

  bool = Boolean;
  int  = Integer;
  float = Single;

  FuncAssignText = function( user_ptr: Pointer;  const data : PAnsiChar) :BOOL; cdecl;


  function DialogSelectFile(const caption : PAnsiChar; const startDir : PWChar;
    const filter : PAnsiChar; dest :PAnsiChar; destsize :integer) : BOOL ;  stdcall;


  function DialogSelectDirectory(const caption : PAnsiChar; const startDir  : PAnsiChar;
						   dest: PAnsiChar; const destsize : integer) : BOOL; stdcall;



  function DialogLogin(const caption : PAnsiChar;
			const loginEditText : PAnsiChar;
			const passwordEditText : PAnsiChar;
			const maxLoginLen : integer;
			const maxPasswordLen : integer;
			desrLogin : PAnsiChar;
			destPassword : PAnsiChar;
			destlen : integer) : BOOL; stdcall;


    function DialogInputLine(const caption :PAnsiChar;
      const line : PAnsiChar; dest :PAnsiChar; const destlen : integer) : BOOL; stdcall;



 	function DialogSelectColor(const caption : PAnsiChar;
    start_red :integer; start_green :integer; start_blue :Integer;
			dest_red :PInteger; dest_green :PInteger; dest_blue :PInteger ) : BOOL; stdcall;



  function DialogSelectItem(const caption :PAnsiChar; const items :PAnsiChar;
		 destSelectItem :pinteger) :BOOL; stdcall;


  function DialogInputText(const caption :PAnsiChar; const startText :PAnsiChar;
		 funcdest :FuncAssignText; userptr :pointer) : BOOL; stdcall;

  procedure Terminate(); stdcall;
  procedure Run(); stdcall;

  function DialogInputVec3( const caption :PAnsiChar; x :PSingle; y  :PSingle; z :PSingle ) :boolean; stdcall;

  function DialogSelectDirShell(const caption :PAnsiChar; dest :PAnsiChar; destlen :integer) :Boolean; stdcall;

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
implementation //---------------------------------------------------------------
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

  uses udm_dialogs, uForm_dialog_login, uFrm_dialog_select_item,
    uFrm_dialog_select_text  , uExportGuiEventCallbackpas,
    uForm_dialog_input_vec3, uForm_selectdirshellview

  ;


function DialogSelectFile(const caption : PAnsiChar; const startDir : PWChar;
  const filter : PAnsiChar; dest :PAnsiChar; destsize :integer) : BOOL ;  stdcall;

  var bres : Boolean;
begin
   dest[0] := AnsiChar(0);

   if udm_dialogs.dm_dialogs = nil then
     begin
       udm_dialogs.dm_dialogs := udm_dialogs.Tdm_dialogs.Create(nil);
     end;

  udm_dialogs.dm_dialogs.dlgOpen_file.Title := caption;
  udm_dialogs.dm_dialogs.dlgOpen_file.InitialDir := startDir;
    udm_dialogs.dm_dialogs.dlgOpen_file.Filter := filter;

   bres := udm_dialogs.dm_dialogs.dlgOpen_file.Execute();

    if(bres) then
    begin
       result := TRUE
    end
       else
          begin
             result := False;
          end;

    if result = True then
     begin
       SysUtils.StrLCopy(dest, PAnsiChar(udm_dialogs.dm_dialogs.dlgOpen_file.FileName), destsize );
     end;

    
    FreeAndNil(udm_dialogs.dm_dialogs);
end;

function DialogSelectDirectory(const caption : PAnsiChar; const startDir  : PAnsiChar;
						   dest: PAnsiChar; const destsize : integer) : BOOL; stdcall;

begin
    dest[0] := AnsiChar(0);

    // udm_dialogs.dm_dialogs.




    ShowMessage('no code');
    Result := False;




end;


function DialogLogin(const caption : PAnsiChar;
			const loginEditText : PAnsiChar;
			const passwordEditText : PAnsiChar;
			const maxLoginLen : integer;
			const maxPasswordLen : integer;
			desrLogin : PAnsiChar;
			destPassword : PAnsiChar;
			destlen : integer) : BOOL; stdcall;

      var  ires : Integer;
begin
   desrLogin[0] := AnsiChar(0);
   destPassword[0] := AnsiChar(0);

   if uForm_dialog_login.frm_dialog_login = nil then
     begin
       uForm_dialog_login.frm_dialog_login :=
         uForm_dialog_login.Tfrm_dialog_login.Create(nil);
     end;

    uForm_dialog_login.frm_dialog_login.Caption := caption;
    uForm_dialog_login.frm_dialog_login.edt_login.Text := loginEditText;
    uForm_dialog_login.frm_dialog_login.edt_password.Text := passwordEditText;


    result := False;
    ires := 0;
    ires :=     uForm_dialog_login.frm_dialog_login.ShowModal();

     if(uForm_dialog_login.frm_dialog_login.m_ModalResult = True)
     then
       begin
         Result := True;
       end
     else
     begin
       result := FALSE;

     end;

     if(Result = FALSE)
     then
       begin
         FreeAndNil(uForm_dialog_login.frm_dialog_login);
         Exit;
       end;

     // copy

     SysUtils.StrLCopy(desrLogin,
     PChar(uForm_dialog_login.frm_dialog_login.edt_login.Text),
     destlen);

     SysUtils.StrLCopy(destPassword,
     PChar(uForm_dialog_login.frm_dialog_login.edt_password.Text),
     destlen);

     FreeAndNil(uForm_dialog_login.frm_dialog_login);

end;


function DialogInputLine(const caption :PAnsiChar;
      const line : PAnsiChar; dest :PAnsiChar; const destlen : integer) : BOOL; 
      stdcall;
var bres : Boolean;
temp :AnsiString;
begin

  temp := line;
  bres := Dialogs.InputQuery(caption,'', temp);
  if(bres = false)
  then
  begin
    result := False;
    Exit;
  end;

  Result := True;
  //
  SysUtils.StrLCopy(dest, PAnsiChar(temp), destlen );


end;

function RGBToColor(R, G, B: Byte): TColor;
begin
  Result := B shl 16 or G shl 8 or R;
end;


function RGBIntsToColor(R, G, B: integer): TColor;
begin
  Result := B shl 16 or G shl 8 or R;
end;

function DialogSelectColor(const caption : PAnsiChar;
    start_red :integer; start_green :integer; start_blue :Integer;
			dest_red :PInteger; dest_green :PInteger; dest_blue :PInteger ) : BOOL; 
      stdcall;

 var cdo :TColorDialogOption;
    color_src : TColor ;
    color_res : TColor ;
    bres : boolean;
 
begin

     color_src := RGBIntsToColor(start_red, start_green, start_blue);
     color_res := 0;

    if dm_dialogs = nil then
     begin
      dm_dialogs := udm_dialogs.Tdm_dialogs.Create(nil);

     end;


    // cdo.
    udm_dialogs.dm_dialogs.dlgColor.Color := color_src;
    bres := udm_dialogs.dm_dialogs.dlgColor.Execute();

    if(bres = false)
    then
    begin
      Result := FALSE;
      FreeAndNil(dm_dialogs);
      Exit;
    end;


    color_res := udm_dialogs.dm_dialogs.dlgColor.Color;

    // copy to dest
    // function RGBIntsToColor(R, G, B: integer): TColor;
    // Result := B shl 16 or G shl 8 or R;

    dest_blue^ := color_res;
    dest_blue^ := dest_blue^ shr 16;
    dest_blue^ := dest_blue^ or  $ffffff00;
    dest_blue^ := dest_blue^ xor $ffffff00;

    dest_green^ := color_res;
    dest_green^ := dest_green^ shr 8;
    dest_green^ := dest_green^ or  $ffffff00;
    dest_green^ := dest_green^ xor $ffffff00;


    dest_red^ := color_res;
    dest_red^ := dest_red^ shr 0;
    dest_red^ := dest_red^ or  $ffffff00;
    dest_red^ := dest_red^ xor $ffffff00;



    FreeAndNil(dm_dialogs);

end;


function DialogSelectItem(const caption :PAnsiChar; const items :PAnsiChar;
		 destSelectItem :pinteger) :BOOL; stdcall;
      var curr : AnsiString;
     var srcitems : TStringList;

begin

  // split string
    // srcString := items;

//     curr := '';
//     For c := 1 to  Length(srcString)  do
//     begin
//         if( srcString[c] = separator )
//         then
//            begin
//               srcitems.Append ( curr );
//               curr := '';
//              Continue;
//            end;
//
//         curr := curr + srcString[c];
//     end;

     if uFrm_dialog_select_item.frmDlgSelItem = nil then
     begin
       uFrm_dialog_select_item.frmDlgSelItem :=
        uFrm_dialog_select_item.TfrmDlgSelItem.Create(nil);

     end;

     srcitems :=  TStringList.Create;
     srcitems.Text := items;
     uFrm_dialog_select_item.frmDlgSelItem.lst1.Items := srcitems;
     srcitems.Free;

    uFrm_dialog_select_item.frmDlgSelItem.lst1.ItemIndex := -1;
 
    uFrm_dialog_select_item.frmDlgSelItem.modal_result := False;
    uFrm_dialog_select_item.frmDlgSelItem.Caption := caption;
    uFrm_dialog_select_item.frmDlgSelItem.ShowModal();

    if(uFrm_dialog_select_item.frmDlgSelItem.modal_result = false)
    then
      begin
        Result := FALSE;
        FreeAndNil(uFrm_dialog_select_item.frmDlgSelItem);
        Exit;
      end;


    Result := TRUE;
    destSelectItem^ := uFrm_dialog_select_item.frmDlgSelItem.lst1.ItemIndex;

    FreeAndNil(uFrm_dialog_select_item.frmDlgSelItem);

end;


function DialogInputText(const caption :PAnsiChar; const startText :PAnsiChar;
		 funcdest :FuncAssignText; userptr :pointer) : BOOL; stdcall;

  var newText : AnsiString;

begin
   if uFrm_dialog_select_text.frm_sel_text = nil then
     begin
       uFrm_dialog_select_text.frm_sel_text :=
        uFrm_dialog_select_text.Tfrm_sel_text.Create(nil);

     end;


   uFrm_dialog_select_text.frm_sel_text.modal_res := false;
   uFrm_dialog_select_text.frm_sel_text.caption := caption;
   uFrm_dialog_select_text.frm_sel_text.mmo.Lines.Clear();
   uFrm_dialog_select_text.frm_sel_text.mmo.Lines.Text  :=  startText;

   uFrm_dialog_select_text.frm_sel_text.ShowModal();

   if(uFrm_dialog_select_text.frm_sel_text.modal_res = false)
   then
    begin
      Result := False;
      FreeAndNil(uFrm_dialog_select_text.frm_sel_text);
      Exit;
    end;

    Result := True;

    // assign
    newText :=  uFrm_dialog_select_text.frm_sel_text.mmo.Lines.Text;
    funcdest(userptr,  PAnsiChar(newText) );


    FreeAndNil(uFrm_dialog_select_text.frm_sel_text);

end;

procedure Terminate(); stdcall;
begin
  Application.Terminate;
  g_helper.process_messages_result := false;

end;

procedure Run(); stdcall;
begin
  Application.Run;
end;


function DialogInputVec3( const caption :PAnsiChar; x :PSingle; y  :PSingle; z :PSingle ) :boolean; stdcall;
   // var  uForm_dialog_input_vec3.
begin
     if uForm_dialog_input_vec3.frmdicalog_input_vec3 = nil then
       begin
         uForm_dialog_input_vec3.frmdicalog_input_vec3 :=
          uForm_dialog_input_vec3.Tfrmdicalog_input_vec3.Create(nil);
       end;

    result := uForm_dialog_input_vec3.frmdicalog_input_vec3.ExecuteDialog(
    caption, x, y, z );



      FreeAndNil(uForm_dialog_input_vec3.frmdicalog_input_vec3 );

end;


function DialogSelectDirShell(const caption :PAnsiChar; dest :PAnsiChar; destlen :integer) :Boolean; stdcall;
  var modres :Integer;
begin
  modres := 0;
  Result := false;

  uForm_selectdirshellview.form_dialog_selectdir_shell :=
     uForm_selectdirshellview.Tform_dialog_selectdir_shell.Create(nil);

  uForm_selectdirshellview.form_dialog_selectdir_shell.Caption := caption;

  uForm_selectdirshellview.form_dialog_selectdir_shell.m_result := '';
  modres := uForm_selectdirshellview.form_dialog_selectdir_shell.ShowModal;

  if uForm_selectdirshellview.form_dialog_selectdir_shell.m_result = '' then
   begin
    FreeAndNil(uForm_selectdirshellview.form_dialog_selectdir_shell);
    Exit;
   end;

   SysUtils.StrLCopy(dest,
      PAnsiChar(uForm_selectdirshellview.form_dialog_selectdir_shell.m_result),
      destlen
      );


  FreeAndNil(uForm_selectdirshellview.form_dialog_selectdir_shell);
  result := true;
end;



end.
